package com.goldgov.branchCircle.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.goldgov.branchCircle.dao.BranchCircleDao;
import com.goldgov.branchCircle.dao.ThumbUpDao;
import com.goldgov.branchCircle.service.BranchCircle;
import com.goldgov.branchCircle.service.ThumbUp;
import com.goldgov.branchCircle.service.ThumbUpService;
import com.goldgov.utils.DataUtil;
import com.goldgov.utils.JiguangPush;
@Service
public class ThumbUpServiceImpl implements ThumbUpService{

	@Autowired
	private ThumbUpDao thumbUpDao;
	@Autowired
	private BranchCircleDao branchCircleDao;
	@Override
	public void saveThumbUp(ThumbUp thumbUp) {
		ThumbUp up=thumbUpDao.getThumbUpByID(thumbUp.getUserID(), thumbUp.getBranchID());
		if(null!=up) {
			Integer status=up.getStatus();
			if(null!=status && status==1) {
				up.setStatus(2);
			}else {
				up.setStatus(1);
				BranchCircle branchCircle = branchCircleDao.getBranchCircle(up.getBranchID());
				if (!thumbUp.getUserID().equals(branchCircle.getUserID())) {
					try {
						Map<String, String> map = new HashMap<>();
						map.put("id", up.getBranchID());
						map.put("sourceType", DataUtil.MESSAGE_TYPE_CUSTOM_UP);
						Map<String, String> params = new HashMap<>();
						params.put("userID", branchCircle.getUserID().replace("-", ""));
						params.put("modelName", up.getUserName()+"对您的"+branchCircle.getBranchContent()+"消息进行了点赞");
						params.put("content", branchCircle.getBranchContent());
						params.put("mapString", JSONObject.toJSONString(map));
						JiguangPush.customJpush(params);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			thumbUpDao.updateThumbUp(up);
		}else {
			thumbUp.setStatus(1);
			thumbUpDao.addThumbUp(thumbUp);
			BranchCircle branchCircle = branchCircleDao.getBranchCircle(thumbUp.getBranchID());
			if (!thumbUp.getUserID().equals(branchCircle.getUserID())) {
				try {
					Map<String, String> map = new HashMap<>();
					map.put("id", thumbUp.getBranchID());
					map.put("sourceType", DataUtil.MESSAGE_TYPE_CUSTOM_UP);
					
					Map<String, String> params = new HashMap<>();
					params.put("userID", branchCircle.getUserID().replace("-", ""));
					params.put("modelName", thumbUp.getUserName()+"对您的"+branchCircle.getBranchContent()+"消息进行了点赞");
					params.put("content", branchCircle.getBranchContent());
					params.put("mapString", JSONObject.toJSONString(map));
					JiguangPush.customJpush(params);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public List<ThumbUp> getThumbUpList(String branchID) {
		return thumbUpDao.getThumbUpList(branchID);
	}

	
	

}
